// 入口文件
// 路由
import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

// 过滤器
import moment from 'moment'

Vue.filter('dataForm', function (dataStr, pattern = "YYYY-MM-DD HH:mm:ss") {
    return moment(dataStr).format(pattern)
})

// mitui
// import { Toast, Header, Button, Swipe, SwipeItem} from 'mint-ui';
// Vue.component(Header.name, Header);
// Vue.component(Button.name, Button);
// Vue.component(Swipe.name, Swipe);
// Vue.component(SwipeItem.name, SwipeItem);
// Vue.component(Toast.name, Toast);
import MintUI from 'mint-ui'

Vue.use(MintUI)
import 'mint-ui/lib/style.css'

import VueResourse from 'vue-resource'

Vue.use(VueResourse)
// 设置请求的根路径
Vue.http.options.root = 'http://vue.studyit.io';
//设置提交格式
Vue.http.options.emulateJSON = true;


// 导入 MUI 的样式
import './lib/mui/css/mui.min.css'
// 导入扩展图标样式
import './lib/mui/css/icons-extra.css'

import router from './router'
import app from './App.vue'

// 懒加载
import VueLazyload from 'vue-lazyload'

Vue.use(VueLazyload, {
    preLoad: 1.3,
    error: 'dist/error.png',
    loading: 'dist/loading.gif',
    attempt: 1
})

//缩略图
import VuePreview from 'vue-preview'

Vue.use(VuePreview)

// Vuex引入
import Vuex from 'vuex'

Vue.use(Vuex)

var car = JSON.parse(localStorage.getItem('car') || '[]')
var store = new Vuex.Store({
    state: {
        car: car
    },
    mutations: {
        addShoppingCar(state, payload) {

            let flag = false
            state.car.some(item =>{
                if(payload.id == item.id){
                    item.count += parseInt(payload.count)
                    flag = true
                }
            })


            if (!flag){
                state.car.push(payload)
            }
            localStorage.setItem('car',JSON.stringify(state.car))
        },
        updateList(state, payload){
            state.car.forEach(item => {
                if(item.id == payload.id){
                    item.count = parseInt(payload.count)
                }
            })
            localStorage.setItem('car',JSON.stringify(state.car))
        },
        removeItem(state, payload){
            state.car.some((item, i) => {
                if(item.id == payload.id){
                    state.car.splice(i, 1)
                }
            })

            localStorage.setItem('car',JSON.stringify(state.car))
        },
        saveSelected(state, payload){
            state.car.some(item => {
                if (item.id == payload.id)
                item.selected = payload.selected
            })
            localStorage.setItem('car',JSON.stringify(state.car))
        }
    },

    getters: {
        getAllCount(state){
            var c = 0
            state.car.forEach( item => {
                if(item.selected){
                    c += parseInt(item.count)
                }


            })
            return c

        },
        getIdAcount(state){
            var L = {}
            state.car.forEach(item => {
                L[item.id] = item.count
            })
            return L
        },
        getSum(state){
            var M = {
                count:0,
                amount:0
            }
            state.car.forEach(item => {
                if(item.selected){
                    M.count = item.count
                    M.amount += item.price * M.count
                }

            })
            return M
        },
        getSelected(state){
            var S ={}
            state.car.forEach(item => {
                S[item.id] = item.selected
            })
            return S
        }

    }
})

var vm = new Vue({
    el: '#app',
    render: c => c(app),
    router,
    store
})

